/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SendView.java
 *
 * Created on 23 mars 2009, 10:48:58
 */

package client;

import Common.Message;
import java.io.File;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.ListModel;

/**
 * The window used to collect informations to send a file.
 * @author riasb
 */
public class SendView extends javax.swing.JFrame {

    /** Creates new form SendView
     * @param pcv
     * @param c
     */
    public SendView(ProjetChatView pcv, ClientNetwork c) {
        initComponents();
		setTitle("Send a Message");
        this.pcv = pcv;
        this.client = c;
        DefaultComboBoxModel list = new DefaultComboBoxModel();
        ListModel model =  pcv.getList().getModel();
        for (int i = 1; i < model.getSize(); i++) {
            list.addElement(model.getElementAt(i));
        }
        NicksComboBox.setModel(list);
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        NicksComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        OkButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        FileTextField = new javax.swing.JTextField();
        BrowseButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        NicksComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        NicksComboBox.setName("NicksComboBox"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(client.ProjetChatApp.class).getContext().getResourceMap(SendView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        OkButton.setText(resourceMap.getString("OkButton.text")); // NOI18N
        OkButton.setName("OkButton"); // NOI18N
        OkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OkButtonActionPerformed(evt);
            }
        });

        CancelButton.setText(resourceMap.getString("CancelButton.text")); // NOI18N
        CancelButton.setName("CancelButton"); // NOI18N
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        FileTextField.setEditable(false);
        FileTextField.setText(resourceMap.getString("FileTextField.text")); // NOI18N
        FileTextField.setName("FileTextField"); // NOI18N

        BrowseButton.setText(resourceMap.getString("BrowseButton.text")); // NOI18N
        BrowseButton.setName("BrowseButton"); // NOI18N
        BrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BrowseButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(NicksComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(FileTextField)
                                .addGap(18, 18, 18)
                                .addComponent(BrowseButton))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(130, 130, 130)
                        .addComponent(OkButton)
                        .addGap(40, 40, 40)
                        .addComponent(CancelButton)))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(NicksComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(FileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BrowseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CancelButton)
                    .addComponent(OkButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
/**
 * The action performed on CancelButton click.
 * @param evt event
 */
    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        pcv.setSendEnabled(true);
        this.dispose();
    }//GEN-LAST:event_CancelButtonActionPerformed

	/**
	 * The action performed on OkButton click.
	 *
	 * @param evt event
	 */
    private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OkButtonActionPerformed
        if ((NicksComboBox.getSelectedItem() != null) && (!FileTextField.getText().isEmpty())) {
            File f = new File(FileTextField.getText());
            if (f.exists() && f.canRead()){
                Message msg = new Message(Message.FILE_PROPOSE, pcv.getNick(), NicksComboBox.getSelectedItem().toString(), f.getName());
                ClientThread ct = client.getClientThread();
                ct.setIsSendingFile(true);
                ct.setFileToSend(f);
                ct.setNickDest(NicksComboBox.getSelectedItem().toString());
                client.sendMessage(msg);
                pcv.setStatus("Waiting for acceptation from " + NicksComboBox.getSelectedItem().toString() + "...");
				pcv.setProgressBarVisible(true);
				pcv.setProgressBarInderterminate(true);
                this.dispose();
            }
            else
                JOptionPane.showMessageDialog(this, "The file doesn't exist or cannot be accessed.", "No such file/Cannot acces to file", JOptionPane.WARNING_MESSAGE);
        }
        else
            JOptionPane.showMessageDialog(this, "Please choose a recipient AND a file.", "No recipient/file choosen", JOptionPane.WARNING_MESSAGE);
    }//GEN-LAST:event_OkButtonActionPerformed

	/**
	 * The action perfomed on BrowseButton click.
	 * @param evt event
	 */
    private void BrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BrowseButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        int response = chooser.showOpenDialog(this);
        if (response == JFileChooser.APPROVE_OPTION) {
            FileTextField.setText(chooser.getSelectedFile().getAbsolutePath());
        }
    }//GEN-LAST:event_BrowseButtonActionPerformed

    /**
     * @param args the command line arguments
     * @param pcv the ProjetChatView that lunched this main.
     * @param c the ClientNetwork corresponding to the ProjetChatView.
    */
    public static void main(String args[], final ProjetChatView pcv, final ClientNetwork c) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SendView(pcv, c).setVisible(true);
            }
        });
    }

    private String file;
    private String nick;
    private ProjetChatView pcv;
    private ClientNetwork client;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BrowseButton;
    private javax.swing.JButton CancelButton;
    private javax.swing.JTextField FileTextField;
    private javax.swing.JComboBox NicksComboBox;
    private javax.swing.JButton OkButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables

}
